import { useMyToastStore } from "@/store/myToast";
const request = () => {
  return {
    get(url, params) {
      return this.request("GET", url, params);
    },
    post(url, params) {
      return this.request("POST", url, params);
    },
    request: async (method, url, params) => {
      // 1995-12-17T03:24:00
      let curTime = Number(uni.$u.timeFormat(new Date(), "hh"));
      const myToast = useMyToastStore();
      if (curTime < 6) {
        setTimeout(() => {
          myToast.params = {
            type: "error",
            message: "服务器关闭，00:00至06:00",
          };
        }, 1500);
        return;
      }
      const { result } = await uniCloud.callFunction({
        name: "daily8am",
        data: {
          path: url,
          method,
          params,
        },
      });
      if (result.data.indexOf("错误提示") > -1) {
        uni.setStorageSync("loginExpired", false);
        setTimeout(() => {
          myToast.params = {
            type: "error",
            message: "登录信息过期，请重新登录",
            complete() {
              uni.navigateTo({
                url: "/pages/user/login",
              });
            },
          };
        }, 0);
      }
      if (result.data[0] == "{") {
        result.data = JSON.parse(result.data);
      }
      return result;
    },
  };
};

export default request();
